﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using ConsoleApp.Job.TaskFramework;
using ConsoleAppFramework.Util;

namespace ConsoleAppFramework
{
    internal class Program
    {

        static void Main(string[] args)
        {

            #region Rmq测试
            //for (int i = 0; i < 100; i++)
            //{
            //    RmqQueueManager.Enqueue($"Rmq测试入队消息{i}", RmqQueueManager.QueueName.测试);
            //    ConsoleHelper.WriteInfoLine($"Rmq测试入队消息{i}");
            //}

            //Thread.Sleep(1000);

            //for (int i = 0; i < 100; i++)
            //{
            //    var fsMsg = new FeiShuGroupMsg
            //    {
            //        Content = $"Rmq飞书消息入队{i}",
            //        FeiShuGroupEnum = FeiShuGroupEnum.测试,
            //    };
            //    RmqQueueManager.Enqueue(fsMsg, RmqQueueManager.QueueName.线上飞书消息);

            //    ConsoleHelper.WriteInfoLine($"Rmq飞书消息入队{i}");
            //}

            #endregion

            #region 单个任务测试
            //args = new List<string> { "测试任务", "ConsoleApp.Job.TaskJob.测试任务", "4" }.ToArray();
            //args = new List<string> { "Rmq测试任务", "ConsoleApp.Job.TaskJob.Rmq测试任务", "4" }.ToArray(); 
            #endregion

            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            ServicePointManager.DefaultConnectionLimit = 512;
            List<ITask> initTask = null;
            try
            {
                if (args.Length >= 3) //发布工具
                {
                    Console.Title = args[0];
                    TaskManager taskManager = new TaskManager(TaskStarter.Init(args[0], args[1], args[2]));
                    taskManager.Start();
                }
                else
                {
                    initTask = TaskStarter.Init();
                    if (initTask.Any())
                    {
                        Console.Title = initTask?.FirstOrDefault()?.Remark;
                        TaskManager taskManager = new TaskManager(initTask);
                        taskManager.Start();
                    }
                }
            }
            catch (Exception ex)
            {
                if (args.Length > 0)
                {
                    Console.WriteLine(args.Length);

                    args.ToList().ForEach(e =>
                    {
                        Console.WriteLine(e);
                    });
                }
                Console.WriteLine(ex.ToString());
            }
            Console.Read();
        }

        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            ConsoleHelper.WriteErrorLine("未知异常:" + (e.ExceptionObject as Exception).Message);
        }
    }
}
